package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.BotanyDao;
import cn.topevery.garden.entity.po.Botany;
import cn.topevery.garden.entity.query.BotanyQuery;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-26
 */
@Repository
public class BotanyDaoImpl extends BaseDaoImpl<Botany> implements BotanyDao {
    @Override
    public <P extends DbCsp> List<Botany> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<Botany> listPage(BotanyQuery query) {
        String table = "t_botany tb";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        String columns = "tb.c_id," +
                "tb.c_flag," +
                "tb.c_code," +
                "tb.c_name," +
                "tb.c_latin_name," +
                "tb.c_area," +
                "tb.c_street," +
                "tb.c_community," +
                "tb.c_distribution_area," +
                "tb.c_total," +
                "tb.c_measure," +
                "tb.c_section," +
                "tb.c_genus," +
                "tb.c_hight," +
                "tb.c_weight," +
                "tb.c_diameter," +
                "tb.c_search_start_time," +
                "tb.c_remark," +
                "tb.c_search_end_time," +
                "tb.c_db_create_date," +
                "tb.c_db_create_id," +
                "tb.c_db_last_update_date," +
                "tb.c_db_last_update_id," +
                "tb.c_db_status ";

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tb.c_db_create_date desc");
        sqlQueryMate
                .where(true, "tb.c_db_status", ScriptConditionEnum.Eq, 0)
                .where(StringUtils.isNotEmpty(query.getCode()), "tb.c_code", ScriptConditionEnum.Like, query.getCode())
                .where(StringUtils.isNotEmpty(query.getName()), "tb.c_name", ScriptConditionEnum.Like, query.getName())
                .where(StringUtils.isNotEmpty(query.getSection()), "tb.c_section", ScriptConditionEnum.Like, query.getSection())
                .where(StringUtils.isNotEmpty(query.getArea()), "tb.c_distribution_area", ScriptConditionEnum.Like, query.getArea())
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, Botany.class);
    }

    /**
     * 描述 检查是否已经存在.
     *
     * @param fieldName 字段名
     * @param value     字段值
     * @return the boolean （true已存在，false不存在）
     * @author wenzhao.lin
     * @created 2021 -08-19 09:23:47 Is exit boolean.
     */
    @Override
    public boolean isExit(String fieldName, String value) {
        String table = "t_botany ";
        String columns = fieldName;
        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns);
        sqlQueryMate
                .where(true, "c_db_status", ScriptConditionEnum.Eq, 0)
                .where(true, fieldName, ScriptConditionEnum.Eq, value)
        ;
        List<Botany> list = selectList(sqlQueryMate,Botany.class);
        if(list != null && list.size()>0){
            return true;
        }else {
            return false;
        }
    }
}
